package com.barkerton.crawler.queuing;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.barkerton.crawler.Page;

public class PageQueue implements Queue<Page> {

	private static final PageQueue SINGLETON = new PageQueue();
	private static final BlockingQueue<Page> QUEUE = 
		new LinkedBlockingQueue<Page>(QUEUE_CAPACITY);
	
	// private constructor requires call to getInstance
	private PageQueue() { }
	
	/**
	 * @return	SINGLETON object
	 */
	public static PageQueue getInstance() {
		return SINGLETON;
	}
	
	/**
	 * @return Current size of the queue
	 */
	public int size() {
		return QUEUE.size();
	}
	
	/**
	 * Removes every element in the entire queue
	 */
	public void clear() {
		QUEUE.clear();
	}
	
	/**
	 * Blocks until the object can be added.
	 * Does not add URL if it already exists in the queue.
	 * 
	 * @param Page object to be added to the queue
	 */
	public void enqueue(Page p) throws InterruptedException {
		if (!QUEUE.contains(p))
			QUEUE.put(p);
	}
	
	/**
	 * Blocks until an item is available to remove
	 * @return Page item from the queue
	 */
	public Page dequeue() throws InterruptedException {
		return QUEUE.take();
	}
	
}
